﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace yq.IBase.Basis
{
    /// <summary>
    /// 外键关联
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    public class ForeignKeyAttribute : Attribute  
    {
        private string _ForeignKey;

        public string ForeignKey
        {
            get { return _ForeignKey; }
        }

        public ForeignKeyAttribute(string  _obj)
        {
            _ForeignKey = _obj;
        }
    }

    /// <summary>
    /// 获取ForeignKeyAttribute上的值
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ForeignKey<T>
    {
        /// <summary>
        /// 根据fullName查找对象上的值
        /// </summary>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public static string Get(string fullName)
        {
            PropertyInfo info = typeof(T).GetProperty(fullName);
            if (info != null)
            {
                ForeignKeyAttribute descriptionAttribute = info.GetCustomAttributes(typeof(ForeignKeyAttribute), true)[0] as ForeignKeyAttribute;

                if (descriptionAttribute != null)
                {
                    return descriptionAttribute.ForeignKey;
                }
            }

            return null;
        }
    }
}
